package com.csthink.mr.serial.mobile;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author <a href="mailto:csthink@icloud.com">Mars</a>
 * @since 2024-04-12 20:10
 */
public class AccessMapper extends Mapper<LongWritable, Text, Text, Access> {

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String line = value.toString();
        String[] splits = line.split("\t");

        //if (splits.length != 9) {
        //    System.out.println("line data is error: " + line);
        //    return;
        //}

        if (splits.length < 3) {
            return;
        }

        String phone = splits[1].trim();
        if (StringUtils.isBlank(phone)) {
            return;
        }


        String upValue = splits[splits.length - 3].trim();
        String downValue = splits[splits.length - 2].trim();
        long up = 0, down = 0;
        if (StringUtils.isNotBlank(upValue)) {
            up = Long.parseLong(upValue);
        }

        if (StringUtils.isNotBlank(downValue)) {
            down = Long.parseLong(downValue);
        }

        context.write(new Text(phone), new Access(phone, up, down));
    }
}
